﻿Imports Microsoft.Win32
'   cftUnity Remote Clients form
'   Copyright (c) 2011 Marvin Westmaas ( MtM / Marvin_The_Martian )
'
'   This program is free software: you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation, either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program.  If not, see <http://www.gnu.org/licenses/>.
Public Class frmRemoteClients
    Private bAllowClose As Boolean = False
    Private bManual As Boolean = False
    Public _IsFirstRun As Boolean = False
    Public Sub FillForm()
        Try
            bManual = True
            lvRClients.Items.Clear()
            For xInt As Int32 = 1 To LogParser.Clients.Count - 1
                Dim rc As clsLogParser.clsClient = LogParser.Clients(xInt)
                Dim nI As New ListViewItem
                nI.Text = rc.ClientName
                nI.SubItems.Add(rc.ClientLocation)
                nI.SubItems.Add(rc.FCPort)
                If rc.FWPort = "" Then
                    nI.SubItems.Add(Boolean.FalseString)
                Else
                    nI.SubItems.Add(Boolean.TrueString)
                End If
                nI.Checked = rc.Enabled
                lvRClients.Items.Add(nI)
            Next
            bManual = False
        Catch ex As Exception
            LogWindow.WriteError(ex.Message, Err)
        End Try
    End Sub
    Private Sub frmRemoteClients_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If e.CloseReason = CloseReason.WindowsShutDown Or e.CloseReason = CloseReason.ApplicationExitCall Then
                bAllowClose = True
            End If
            sqData.SaveRemoteClients()
            If _IsFirstRun Then
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()
            ElseIf Not bAllowClose Then
                e.Cancel = True
                Me.Hide()
            End If
        Catch ex As Exception
            LogWindow.WriteError(ex.Message, Err)
        End Try
    End Sub
    Public Sub CloseForm()
        Try
            bAllowClose = True
            Me.Close()
        Catch ex As Exception
            LogWindow.WriteError(ex.Message, Err)
        End Try
    End Sub

    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
        Try
            addClient.ShowForm(Me)
            FillForm()
        Catch ex As Exception
            LogWindow.WriteError(ex.Message, Err)
        End Try
    End Sub

    Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click
        Try
            If IsNothing(lvRClients.SelectedItems) Then Exit Sub : If Not Me.Created Then Exit Sub
            If lvRClients.SelectedItems.Count = 0 Then Exit Sub
            If lvRClients.SelectedItems(0).Text = LogParser.LocalClient.ClientName Then
                MsgBox("The local client can not be removed.", vbInformation)
                Exit Sub
            End If
            MsgBox("Removing a remote client will not remove the work unit history already stored in the database.")
            rClients.RemoveRemoteClient(lvRClients.SelectedItems(0).Text)
            sqData.SaveRemoteClients()
            lvRClients.Items.RemoveAt(lvRClients.SelectedIndices(0))
        Catch ex As Exception
            LogWindow.WriteError(ex.Message, Err)
        End Try
    End Sub

    Private Sub lvRClients_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lvRClients.ItemCheck
        Try
            If bManual Then Exit Sub : If Not Me.Created Then Exit Sub
            Dim strName As String = lvRClients.Items(e.Index).Text
            If e.NewValue = CheckState.Checked Then
                rClients.SetState(strName, True)
            Else
                rClients.SetState(strName, False)
            End If
        Catch ex As Exception
            LogWindow.WriteError(ex.Message, Err)
        End Try
    End Sub

    Private Sub frmRemoteClients_HelpButtonClicked(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        Try
            ''Will add dialogs for help texts later
            MsgBox("Help text comes here")
        Catch ex As Exception
            LogWindow.WriteError(ex.Message, Err)
        End Try
    End Sub
    Private Sub frmRemoteClients_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

        Catch ex As Exception
            LogWindow.WriteError(ex.Message, Err)
        End Try
    End Sub

   
    Private Sub cmdClose_Click(sender As System.Object, e As System.EventArgs) Handles cmdClose.Click
        If _IsFirstRun Then
            'Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.CloseForm()
        Else
            Me.Close()
        End If
    End Sub
End Class